package bc.running.forum.db;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import bc.running.forum.db.DBType;

public class Config {
	
	public Config() {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(new File("config/config.properties")));
			filter = properties.getProperty("FILTER");
			String dbType = (String)properties.get("dbtype");
			type = DBType.getFromString(dbType);
			user = properties.getProperty(dbType + ".user");
			host = properties.getProperty(dbType + ".host");
			port = Integer.parseInt(properties.getProperty(dbType + ".port"));
			dbName = properties.getProperty(dbType + ".dbname");
			mysqlReservedWords = properties.getProperty("mysql.reserved_words");
			String zipoutput = properties.getProperty("zipoutput");
			if(zipoutput != null && zipoutput.equalsIgnoreCase("true")) {
				this.zipoutfile = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		} 
	}
	
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public DBType getType() {
		return type;
	}
	public void setType(DBType type) {
		this.type = type;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getDbName() {
		return dbName;
	}
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	
	public String getMysqlReservedWords() {
		return mysqlReservedWords;
	}

	public void setMysqlReservedWords(String mysqlReservedWords) {
		this.mysqlReservedWords = mysqlReservedWords;
	}
	
	public boolean isZipoutfile() {
		return zipoutfile;
	}

	public void setZipoutfile(boolean zipoutfile) {
		this.zipoutfile = zipoutfile;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}
	
	public List<String> getWhiteList() {
		if(whiteList == null) {
			String [] whiteListArray = filter.split(",");
			whiteList = Arrays.asList(whiteListArray);
		}
		return whiteList;
	}


	@Override
	public String toString() {
		return " [user=" + user + ", type="
				+ type + ", host=" + host + ", port=" + port + ", dbName="
				+ dbName + "]";
	}
	private List<String> whiteList;
	private boolean zipoutfile;
	private String user;
	private String password;
	private DBType type;
	private String host;
	private int port;
	private String dbName;
	private String mysqlReservedWords;
	private String filter;

}
